#define _CRT_SECURE_NO_WARNINGS 1
#include"string.h"
namespace itimate
{
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n + 1];
			strcpy(tmp, _str);
			delete[]_str;
			_str = tmp;
			_capacity = n;
		}
	}


	void string::push_back( char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity==0?4:_capacity * 2);
		}
		_str[_size] =ch;
		++_size;
		_str[_size] = '\0';
	}



	string& string::operator+= (char c)
	{
		push_back(c);
		return *this;
	}


	string& string::append(const char* s)
	{
		int len = strlen(s);
		if (_size + len > _capacity)
		{
			reserve(_size + len > _capacity * 2 ? _size + len : _capacity * 2);
		}
		for (int i = 0; i < len; i++)
		{
			_str[_size++] = s[i];
		}
		_str[_size]= '\0';
		return *this;
	}


	string& string::operator+= (const char* s)
	{
		append(s);
		return *this;
	}

	void string::resize(size_t n, char ch )
	{
		if (n > _capacity)
		{
			reserve(n);
			for (int i = 0; i < n-_size; i++)
			{
				_str[_size++] = 'ch';
			}
			_str[_size] = '\0';
		}
	}
	bool string::operator==(const string& s)
	{
		return strcmp(_str, s._str) == 0;
	}


	bool string::operator<(const string& s)
	{
		return strcmp(_str, s._str) < 0 ;
	}


	bool string::operator<=(const string& s)
	{
		return  *this<s||*this==s;
	}


	bool string::operator>(const string& s)
	{
		return strcmp(_str, s._str) > 0;
	}

	bool string::operator>=(const string& s)
	{
		return *this>s||*this==s;
	}

	bool string::operator!=(const string& s)
	{
		return !(*this == s);
	}

	char& string::operator[](size_t index)
	{
		assert(index < _size);
		return _str[index];
	}

	const char& string::operator[](size_t index)const
	{
		assert(index < _size);
		return _str[index];
	}
	

	size_t string::find(char c, size_t pos) const
	{
		assert(pos < _size);
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == c)
			{
				return i;
			}
		}
		return npos;
	}

	size_t string::find(const char* str, size_t pos) const
	{
		assert(pos < _size);

		const char* ptr = strstr(_str + pos, str);
		if (ptr == nullptr)
		{
			return npos;
		}
		else
		{
			return ptr - _str;
		}
	}

	void string::insert(size_t pos, char c)
	{
		assert(pos <= _size);
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : _capacity * 2);
		}
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			--end;
		}

		_str[pos] = c;
		++_size;
		
	}

	void string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);
		int len = strlen(str);
		if (_size + len > _capacity)
		{
			reserve(_size + len > _capacity * 2 ? _size + len : _capacity * 2);
		}
		size_t end = _size + len;
		while (end >= pos+len-1)
		{
			_str[end] = _str[end - len];
			--end;
		}
		for (size_t i = 0; i < len; i++)
		{
			_str[pos+i] = str[i];
		}
		_size += len;
	}

	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len > _size - pos)
		{
			_str[pos] = '\0';
			_size -= len;
		}
		else
		{
			for (size_t i = pos + len; i < _size + 1; i++)
			{
				_str[i - len] = _str[i];
			}
			_size -= len;
		}
	}

	string string::substr(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len > _size - pos)
		{
			len = _size - pos;
		}
		string sub;
		sub.reserve(len);
		for (size_t i = 0; i < len; i++)
		{
			sub += _str[pos + i];
		}
		return sub;
	}

	void string::swap(string& s2)
	{
		std::swap(_str, s2._str);
		std::swap(_size, s2._size);
		std::swap(_capacity, s2._capacity);
	}
}

ostream& itimate::operator<<(ostream& out, const string& s)
{
	for (auto ch : s)
	{
		out << ch;
	}

	return out;
}


istream& itimate::operator>>(istream& in, string& s)
{
	s.clear();
	const int N = 256;
	int i = 0;
	char buffet[N];
	char ch;
	ch=in.get();
	while (ch != ' ' && ch != '\n')
	{
		buffet[i++] = ch;
		if (i == N - 1)
		{
			buffet[i] = '\0';
			s += buffet;
			i = 0;
		}
		ch = in.get();
	}
	if (i > 0)
	{
		buffet[i] = '\0';
		s += buffet;
	}
	return in;
}